package com.weiyi.chat.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.weiyi.chat.common.ActivityController;
import com.weiyi.chat.common.Constant;
import com.weiyi.chat.ui.activity.LoginActivity;
import com.weiyi.chat.utils.PreferenceUtil;

import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by YY on 2017/1/4.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected BaseActivity mAct;
    /**
     * 判断网络是否连接正常
     */
    public boolean connect = false;
    private boolean isFirstInit = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutResId() != 0){
            setContentView(getLayoutResId());
        }
        mAct = this;
        ActivityController.addActivity(this);
        initView();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstInit) {
            isFirstInit = false;
            initData();
        }
    }

    protected abstract int getLayoutResId();
    protected abstract void initView();
    protected abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityController.removeActivity(this);

    }

    @OnClick
    public void back(View view){
        finish();
    }

    public void toLogin() {
        PreferenceUtil.setEditB(Constant.SP_ISLOGIN, false, mAct);
        PreferenceUtil.clearAll(mAct);
        jumpToActivity(LoginActivity.class);
        ActivityController.finishIgnoreTag("com.weiyi.chat.ui.activity.LoginActivity");
    }
    /**
     * 抽取积累的方法跳转界面
     */
    protected void jumpToActivity(Class<? extends Activity> actClass) {
        Intent intent = new Intent(mAct, actClass);
        startActivity(intent);
    }

    /**
     * 抽取积累的方法跳转界面 并且将自己关闭
     */
    protected void jumpToActivityAndFinish(Class<? extends Activity> actClass) {
        jumpToActivity(actClass);
        finish();
    }

    /**
     * 获取网络状态
     */
    public boolean checkNet()//网络是否已经连接上
    {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        Log.i("Connect",connect+"///判断网络状态");
        return connect = info != null && info.isConnected();

    }


}
